package com.sci99.ex.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;

public class VideoUtil {
	static Logger log = Logger.getLogger(VideoUtil.class);

	static final String url = "jdbc:sqlserver://192.168.7.45:1433;databaseName=SVOD";
	static final String sql = "select * from Svod_TodayZc_VideoList where type=2 and id = ?";

	public static String getVideoURL(Long cid, String url) {
		if (url == null)
			return "";
		String id = url.substring(url.indexOf("id=") + 3, url.length());
		String newurl = "video.jsp?v=" + id + "&c=" + cid;
		return newurl;
	}

	public static String getFlvURL(String ourl) {
		if (ourl == null)
			return "";
		String id = ourl.substring(ourl.indexOf("id=") + 3, ourl.length());
		return getVideo(NumberUtils.toLong(id)).get("VideoUrl");
	}

	public static Map<String, String> getVideo(Long id) {
		Connection con = null;
		ResultSet rs = null;
		Map<String, String> m = new HashMap<>(8, 1);
		try {
			con = DriverManager.getConnection(url, "admin", "hot99123");
			PreparedStatement ps = con.prepareStatement(sql,
					ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
			ps.setLong(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				m.put("VideoUrl", rs.getString("VideoUrl"));
				m.put("ImageUrl", rs.getString("ImageUrl"));
			}
		} catch (SQLException se) {
			log.error("", se);
		} finally {
			try {
				rs.close();
				con.close();
			} catch (Exception inr) {
				log.error("", inr);
			}
		}
		return m;
	}
}
